<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2016－03－21 http://www.cencn.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 林群伟  <56583965@qq.com> <http://www.xzqly.com>
// +----------------------------------------------------------------------

namespace Common\Model;

use Think\Model;

/**
 * 用户模型
 * @author 林群伟
 */
class MemberModel extends Model {

    /**
     * @param type $uid
     */
    public function login($uid) {

        $user = S("C_USER_INFO_" . $uid);  //当登出错时，把这清空
        if (!$user) {
            $user = $this->where(array('uid' => $uid))->find();
        }
        if (!$user || 1 != $user['status']) {
            return -1003;
        }

        S("C_USER_INFO_" . $uid, $user);
        $this->autoLogin($user);
        return true;
    }

    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    public function autoLogin($user) {
        /* 更新登录信息 */
        $data = array(
            'uid' => $user['uid'],
            'login' => array('exp', '`login`+1'),
            'last_login_time' => NOW_TIME,
            'last_login_ip' => get_client_ip(1),
        );
        $this->save($data);

        /* 记录登录SESSION和COOKIES */
        $auth = array(
            'uid' => $user['uid'],
            'username' => $user['nickname'],
            'last_login_time' => $user['last_login_time'],
        );

        session('lqwsys_auth', $auth);
        session('lqwsys_auth_sign', data_auth_sign($auth));
    }

    public function logout() {
        session('lqwsys_auth', NULL);
        session('lqwsys_auth_sign', NULL);
    }

    public function getNickName($uid) {

        return $this->where(array('uid' => (int) $uid))->getField('nickname');
    }

}
